package dao;


import entity.JobInfo;
import utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JobInfoDao implements BaseDao<JobInfo> {

    public int selectCount() throws SQLException, ClassNotFoundException {
       int count = 0;
        Connection connection = JDBCUtils.openConn();
        String sql = "SELECT COUNT(*) AS allcount FROM job_info";
        CallableStatement statement = connection.prepareCall(sql);

        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()){

            count = resultSet.getInt("allcount");
        }
        statement.close();
        connection.close();
        return count;

    }

    public List<JobInfo> selectByPage(int pageNum,int pageSize,String jobKey,String cityNum) throws SQLException, ClassNotFoundException {
        List<JobInfo> jobInfos = new ArrayList<>();
        Connection connection = JDBCUtils.openConn();
        String sql = "CALL select_by_page (?,?,?,?)";
        CallableStatement statement = connection.prepareCall(sql);
        statement.setInt(1,pageNum);
        statement.setInt(2,pageSize);
        statement.setString(3,jobKey);
        statement.setString(4,cityNum);
        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()){

             String jobId = resultSet.getString("job_id");
             String jobName = resultSet.getString("job_name");
             String jobArea = resultSet.getString("job_area");
             String jobCompany = resultSet.getString("job_Company");
             String companyDesc1 = null;
             String companyDesc2 = null;
             String companyDesc3 = null;
             String jobSalary = resultSet.getString("job_Salary");
             String jobYear = resultSet.getString("job_year");
             String jobXuli = null;
             String searchKw = resultSet.getString("search_Kw");
             String searchCity = resultSet.getString("search_City");
             JobInfo jobInfo = new JobInfo(jobId,jobName,jobArea,jobCompany,companyDesc1,
                     companyDesc2,companyDesc3,jobSalary,jobYear,jobXuli,searchKw,searchCity);
            jobInfos.add(jobInfo);
        }

        statement.close();
        connection.close();
        return jobInfos;
    }

    @Override
    public boolean insert(JobInfo jobInfo) throws SQLException, ClassNotFoundException {
        return false;
    }

    @Override
    public boolean deleteById(int id) throws SQLException, ClassNotFoundException {
        return false;
    }

    @Override
    public boolean update(JobInfo jobInfo) throws SQLException, ClassNotFoundException {
        return false;
    }

    @Override
    public List<JobInfo> selectAll() throws SQLException, ClassNotFoundException {
        return null;
    }

    @Override
    public JobInfo selectById(int id) throws SQLException, ClassNotFoundException {
        return null;
    }

    @Override
    public int batchInsert(List<JobInfo> jobInfoList) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.openConn();
        String sql = "insert into job_info (job_id,job_name,job_area,job_Company," +

                "job_Salary,search_kw,search_city) values (?,?,?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        for(JobInfo jobInfo:jobInfoList){
            statement.setString(1,jobInfo.getJobId());
            statement.setString(2,jobInfo.getJobName());
            statement.setString(3,jobInfo.getJobArea());
            statement.setString(4,jobInfo.getJobCompany());

            statement.setString(5,jobInfo.getJobSalary());
            statement.setString(6,jobInfo.getSearchKw());
            statement.setString(7,jobInfo.getSearchCity());

            statement.addBatch();
        }
        statement.executeBatch();

        statement.close();
        connection.close();
        return 0;
    }
}
